package com.online.usermanagesystem.Interceptor;

import com.online.usermanagesystem.service.ConfigManageService;
import com.online.usermanagesystem.service.EmployeeManageService;
import com.online.usermanagesystem.utils.ResultUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Aspect
@Component
public class CascadeDeleteAop {

    @Autowired
    private ConfigManageService configManageService;

    @Autowired
    private EmployeeManageService employeeManageService;

    @Pointcut("execution(* com.online.usermanagesystem.service.impl.CompanyManageServiceImpl.del(..))")
    private void CompanyDeleting() {
    }

    @Around("CompanyDeleting()")
    public Object doCompanyDeleting(ProceedingJoinPoint pjp) throws Throwable {
        Integer id = (Integer) pjp.getArgs()[0];
        if (null == id) {
            return ResultUtil.BAD_REQUEST;
        }

        Object obj = employeeManageService.listByCompany(id, null, 0, 1);
        if (false == ResultUtil.isSuccess(obj)) {
            return obj;
        }

        if (0 != ((List) obj).size()) {
            return ResultUtil.NOT_Empty;
        }

        return pjp.proceed();
    }

    @Pointcut("execution(* com.online.usermanagesystem.service.impl.EmployeeManageServiceImpl.del(..))")
    private void EmployeeDeleting() {
    }

    @Around("EmployeeDeleting()")
    public Object doEmployeeDeleting(ProceedingJoinPoint pjp) throws Throwable {
        Integer id = (Integer) pjp.getArgs()[0];
        if (null == id) {
            return ResultUtil.BAD_REQUEST;
        }

        Object obj = configManageService.list(id);
        if (false == ResultUtil.isSuccess(obj)) {
            return obj;
        }

        if (0 != ((List) obj).size()) {
            return ResultUtil.NOT_Empty;
        }

        return pjp.proceed();
    }
}
